//
//  LoginViewController.m
//  大食堂
//
//  Created by rimi on 16/1/29.
//  Copyright © 2016年 rimi. All rights reserved.
//

#import "LoginViewController.h"

@interface LoginViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>{
    UIImagePickerController  *_imagePickerController;/**< 图片拾取器 */
    NSMutableArray *_imageNames;
}
@property (strong, nonatomic)UIImageView        *imageView;/**<图片视图 */

@property (strong,nonatomic)UIButton            *loginBtn;/**< 登录按钮 */
@property (strong,nonatomic)UIButton            *imgPickerBtn;/**< 图片拾取按钮 */
@end

@implementation LoginViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//LHYGuideInterfaceView *willEnter = [[LHYGuideInterfaceView alloc]initWithFrame:self.view.frame imageDataArray:_imageNames tintColorType:LHYGuideInterfaceViewButtonColorWithBlack];
//    [self.view addSubview:willEnter];
//    NSLog(@"fgvbhjnkml,");
   
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initializeDataSource];

    
    [self.view addSubview:self.imageView];
    [self.view addSubview:self.loginBtn];
    [self.view addSubview:self.imgPickerBtn];
    
    _imagePickerController    = [[UIImagePickerController alloc]init];
    _imagePickerController.delegate = self;
}
- (void)initializeDataSource
{
    _imageNames = [[NSMutableArray alloc] initWithObjects:@"imageone.png", @"imagetwo.png", nil];
}




#pragma mark - Responds events
- (void)respondsToButton:(UIButton *)sender {
//    switch (sender.tag) {
            // 图片选取
//        case 100:
//        {
            NSArray  *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
                
                _imagePickerController.sourceType =UIImagePickerControllerSourceTypePhotoLibrary ;
                NSLog(@"%@",mediaTypes);
                _imagePickerController.mediaTypes = @[mediaTypes[0]];
                _imagePickerController.allowsEditing = YES;
    
                [self presentViewController:_imagePickerController animated:YES completion:nil];
                
                
            }
//        }
//            break;
//        case 101: {
//            MenuViewController  *menuViewController  = [[MenuViewController alloc]init];
//            [self presentViewController:menuViewController animated:YES completion:nil];
//        }
//            break;
//            
//        default:
//            break;
//    }
}



#pragma mark --UINavigationControllerDelegate UIImagePickerControllerDelegate

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSLog(@"%@",picker);
    NSLog(@"%@",info);
    UIImage  *editedImage = info[@"UIImagePickerControllerEditedImage"];
    self.imageView.image  = editedImage;
    [self.presentedViewController dismissViewControllerAnimated:YES
                                                     completion:nil];
}

#pragma mark  -- Getter  method

-(UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.bounds = CGRectMake(0, 0, 220, 220);
        _imageView.center = CGPointMake(CGRectGetMidX(self.view.bounds), 160);
        _imageView.backgroundColor = [UIColor cyanColor];
        
    }
    return _imageView;
}


- (UIButton *)loginBtn {
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _loginBtn.bounds = CGRectMake(0, 0, 100, 30);
        _loginBtn.center = CGPointMake(CGRectGetMidX(self.view.bounds) + 30, CGRectGetMaxY(self.imageView.frame) + CGRectGetMidX(_loginBtn.bounds) + 50);
        _loginBtn.tag = 101;
        [_loginBtn setTitle:@"点击登陆" forState:UIControlStateNormal];
        [_loginBtn addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}

- (UIButton *)imgPickerBtn {
    if (!_imgPickerBtn) {
        _imgPickerBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _imgPickerBtn.bounds = CGRectMake(0, 0, 100, 30);
        _imgPickerBtn.center = CGPointMake(CGRectGetMidX(self.view.bounds) - 50, CGRectGetMidY(self.loginBtn.frame));
        _imgPickerBtn.tag = 100;
        [_imgPickerBtn setTitle:@"选择头像" forState:UIControlStateNormal];
        [_imgPickerBtn addTarget:self action:@selector(respondsToButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _imgPickerBtn;
    
    
}
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0){
    return YES;
}




@end
